package com.three.lab.page.goods;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.three.lab.R;
import com.three.lab.adapter.FootPrintAdapter;
import com.three.lab.base.AppConstant;
import com.three.lab.base.BaseActivity;
import com.three.lab.bean.FootDataRespon;
import com.three.lab.bean.FootPrintBean;
import com.three.lab.mock.FootPrintData;
import com.three.lab.util.GsonUtils;
import com.three.lab.util.ToastUtils;

import java.util.List;

import static com.three.lab.util.RecyclerViewAnimation.runLayoutAnimation;

/**
 * 浏览足迹
 * created by lqd on 2024-07-14.
 */
public class FootPrintActivity extends BaseActivity {

    private Toolbar toolbar;
    private TextView tvTitle, tvLoading,tvCheckAll,tvDelete,tvEdit;
    private View mLoadingView;
    private ImageView ivState;
    private ProgressBar progressBar;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private FootPrintAdapter adapter;
    private LinearLayout layBottom;

    private static final int STATE_DEFAULT = 0;//默认状态
    private static final int STATE_EDIT = 1;//编辑状态
    private int mEditMode = STATE_DEFAULT;
    private boolean editorStatus = false;//是否为编辑状态
    private int index = 0;//当前选中的item数

    private int pageNo = 1;
    private boolean isloadMore = false;

    @Override
    protected void initParms(Bundle bundle) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_foot_print;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        setStatusBar(R.color.colorPrimary);
        toolbar = $(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(getString(R.string.footprints));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
       // tvTitle = $(R.id.tv_title);
       // tvTitle.setText(getString(R.string.footprints));

        layBottom = $(R.id.lay_bottom);
        tvCheckAll = $(R.id.tv_check_all);
        tvDelete = $(R.id.tv_delete);
        tvEdit = $(R.id.tv_edit);

        refreshLayout = $(R.id.refresh_layout);
        //禁用下拉和上拉
        //refreshLayout.setEnableRefresh(false);
       // refreshLayout.setEnableLoadMore(false);

        mLoadingView = LayoutInflater.from(this).inflate(R.layout.loading_view, null);
        ivState = mLoadingView.findViewById(R.id.iv_state);
        tvLoading = mLoadingView.findViewById(R.id.tv_loading);
        progressBar = mLoadingView.findViewById(R.id.progressBar);

        recyclerView = $(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new FootPrintAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        $(R.id.tv_edit).setOnClickListener(this);
        $(R.id.tv_check_all).setOnClickListener(this);
        $(R.id.tv_delete).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onRefreshing();
            }
        });
        tvLoading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefreshing();
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (isloadMore) {
                            pageNo++;
                            loadData();
                        }
                    }
                }, 1000);
            }
        }, recyclerView);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                if (editorStatus) {//编辑状态
                    FootPrintBean bean = (FootPrintBean) baseQuickAdapter.getItem(position);
                    boolean isSelect = bean.isSelect();
                    if (!isSelect) {
                        index++;
                        bean.setSelect(true);
                    } else {
                        bean.setSelect(false);
                        index--;
                    }
                    if (index == 0) {
                        tvDelete.setText("删除");
                    } else {
                        tvDelete.setText("删除(" + index + ")");
                    }
                    baseQuickAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_edit://编辑
                updateEditState();
                break;
            case R.id.tv_check_all://全选
                setAllItemChecked();
                break;
            case R.id.tv_delete://删除
                deleteCheckItem();
                break;
        }
    }

    private void updateEditState() {
        mEditMode = mEditMode == STATE_DEFAULT ? STATE_EDIT : STATE_DEFAULT;
        if (mEditMode == STATE_EDIT) {
            tvEdit.setText("取消");
            layBottom.setVisibility(View.VISIBLE);
            editorStatus = true;
        } else {
            tvEdit.setText("编辑");
            layBottom.setVisibility(View.GONE);
            editorStatus = false;

            setAllItemUnchecked();//取消全选
        }
        adapter.setEditMode(mEditMode);
    }

    private void setAllItemChecked() {
        if (adapter == null) return;
        List<FootPrintBean> mlist = adapter.getData();
        for (int i = 0; i < mlist.size(); i++) {
            mlist.get(i).setSelect(true);
        }
        adapter.notifyDataSetChanged();
        index = mlist.size();
        tvDelete.setText("删除(" + index + ")");
    }

    //取消全部选中
    private void setAllItemUnchecked() {
        if (adapter == null) return;
        List<FootPrintBean> mList = adapter.getData();
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setSelect(false);
        }
        adapter.notifyDataSetChanged();
        tvDelete.setText("删除");
        index = 0;
    }

    private void deleteCheckItem() {
        if (adapter == null) return;
        if (index == 0) {
            ToastUtils.showToast("请至少选择一条数据再删除");
            return;
        }
        List<FootPrintBean> mList = adapter.getData();
        for (int i = mList.size() - 1; i >= 0; i--) {
            if (mList.get(i).isSelect() == true) {
                mList.remove(i);
            }
        }

        //删除选中的item之后判断是否还有数据，没有则退出编辑模式
        if (mList.size() != 0) {
            index = 0;//删除之后置为0
            tvDelete.setText("删除");
        }else {
            onRefreshing();
        }

        adapter.notifyDataSetChanged();
        runLayoutAnimation(recyclerView);//动画显示
        ToastUtils.showToast(getString(R.string.operate_success));
    }

    @Override
    protected void initData(Context mContext) {
        onRefreshing();
    }

    private void onRefreshing() {
        index = 0;
        mEditMode = STATE_DEFAULT;//恢复默认状态
        editorStatus = false;//恢复默认状态
        tvDelete.setText("删除");
        tvEdit.setText("编辑");
        tvEdit.setVisibility(View.VISIBLE);//显示编辑
        layBottom.setVisibility(View.GONE);
        adapter.setEditMode(STATE_DEFAULT);

        progressBar.setVisibility(View.VISIBLE);
        tvLoading.setText(getString(R.string.loading));
        ivState.setVisibility(View.GONE);
        adapter.setEnableLoadMore(true);
        adapter.getData().clear();
        if (refreshLayout != null && refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(true);
        }
        pageNo = 1;
        isloadMore = false;
       // adapter.notifyDataSetChanged();
        loadData();
    }

    private void loadData() {
        adapter.setEmptyView(mLoadingView);
        String result = FootPrintData.JSON;
        FootDataRespon dataRespon = GsonUtils.parseJsonWithGson(result,FootDataRespon.class);
        List<FootPrintBean> mlist = dataRespon.getData();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                onResetRefresh();
                setListAdapter(mlist);//设置页面数据
            }
        }, 1000);
    }

    private void setListAdapter(List<FootPrintBean> mlist) {
        if (isloadMore) {
            adapter.addData(mlist);
            if (mlist.size() < AppConstant.DEFAULT_PAGE_SIZE) {
                adapter.loadMoreEnd();
            } else {
                adapter.loadMoreComplete();
                adapter.setEnableLoadMore(true);
            }
        } else if (mlist.isEmpty()) {
            adapter.loadMoreEnd();
            adapter.setEnableLoadMore(false);
            setState(getString(R.string.no_data), R.mipmap.ic_empty);
        } else {
            adapter.setNewData(mlist);
            adapter.loadMoreComplete();
            adapter.setEnableLoadMore(true);

            isloadMore = false;
            adapter.loadMoreEnd();
        }
    }

    private void setState(String string, int resId) {
        progressBar.setVisibility(View.GONE);
        tvLoading.setText(string);
        ivState.setImageResource(resId);
        ivState.setVisibility(View.VISIBLE);
        adapter.setEmptyView(mLoadingView);
        adapter.notifyDataSetChanged();
    }

    private void onResetRefresh() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        progressBar.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onResetRefresh();
    }
}